<template>
    <div>
        <!-- 三级分类 -->
        <Category :scene="scene" />

        <el-card style="margin: 10px 0px;">
            <div v-show="scene == 0">
                <el-button type="primary" size="default" icon="Plus" :disabled="categoryStore.c3Id ? false : true"
                    @click="addSpu">添加SPU</el-button>
                <el-table style="margin: 10px 0px;" border :data="records">
                    <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
                    <el-table-column label="SPU名称" prop="spuName"></el-table-column>
                    <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
                    <el-table-column label="SPU操作">
                        <template #="{ row }">
                            <el-button type="primary" size="small" icon="Plus" title="添加SKU"
                                @click="addSku(row)"></el-button>
                            <el-button type="primary" size="small" icon="Edit" title="修改SPU"
                                @click="updateSpu(row)"></el-button>
                            <el-button type="primary" size="small" icon="View" title="查看SKU列表"
                                @click="findSku(row)"></el-button>
                            <el-popconfirm :title="`您确定删除${row.spuName}吗？`" width="200px" @confirm="deleteSpu(row)">
                                <template #reference>
                                    <el-button type="primary" size="small" icon="Delete" title="删除SPU"></el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- 分页器 -->
                <el-pagination @size-change="changeSize" @current-change="getHasSpu" v-model:current-page="pageNo"
                    v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9]" :background="true"
                    layout=" prev, pager, next, jumper,->, sizes,total" :total="total" />
            </div>
            <!-- 添加spu和修改spu子组件 -->
            <SpuForm ref="spu" v-show="scene == 1" @changeScene="changeScene" />
            <!-- 添加sku子组件 -->
            <SkuForm ref="sku" v-show="scene == 2" @changeScene="changeScene" />
            <!-- dialog对话框 -->
            <el-dialog v-model="show" title="SKU列表">
                <el-table border :data="skuArr">
                    <el-table-column label="SKU名字" prop="skuName"></el-table-column>
                    <el-table-column label="SKU价格" prop="price"></el-table-column>
                    <el-table-column label="SKU重量" prop="weight"></el-table-column>
                    <el-table-column label="SKU图片">
                        <template #="{ row }">
                            <img :src="row.skuDefaultImg" alt="" style="width: 100px; height: 100px;">
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>
        </el-card>
    </div>
</template>

<script setup lang="ts">
import { ref, watch, onBeforeUnmount } from 'vue';
import { reqHasSpu, reqSkuList, rqRemoveSpu } from '@/api/product/spu/index'
import SkuForm from './skuForm.vue'
import SpuForm from './spuForm.vue'
import useCategoryStore from '@/store/modules/category'
import { ElMessage } from 'element-plus';
let categoryStore: any = useCategoryStore()
// 场景切换的数据
let scene = ref(0)
let pageNo = ref(1)
let pageSize = ref(3)
let total = ref(0)
// 拿到spuform组件实例
let spu = ref()
// 拿到skuform组件实例
let sku = ref()
// 存储已有的spu的数据
let records = ref([])
// 存储全部sku的数据
let skuArr = ref([])
// 对话框显示与隐藏
let show = ref(false)
watch(() => categoryStore.c3Id, () => {
    // 保证三级id得有了才能发送请求
    if (!categoryStore.c3Id) return
    getHasSpu()
})

// 获取三级分类下的已有的spu
const getHasSpu = async (pager = 1) => {
    pageNo.value = pager
    let result = await reqHasSpu(pageNo.value, pageSize.value, categoryStore.c3Id)
    if (result.code == 200) {
        records.value = result.data.records
        total.value = result.data.total
    }
}
// 分页器下拉菜单的回调
const changeSize = () => {
    getHasSpu()
}
// 添加spu按钮的回调
const addSpu = () => {
    scene.value = 1
    spu.value.initAddSpu(categoryStore.c3Id)
}
// skuform子组件的自定义事件
const changeScene = (obj: any) => {
    scene.value = obj.flag
    if (obj.params == 'update') {
        // 更新留在当前页
        getHasSpu(pageNo.value)
    } else {
        // 添加留在第一页
        getHasSpu()
    }
}
// 修改spu按钮的回调
const updateSpu = (row: any) => {
    scene.value = 1
    spu.value.initHasSpuData(row)
}
// 添加sku按钮的回调
const addSku = (row: any) => {
    scene.value = 2
    sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row)
}
// 查看sku列表的数据
const findSku = async (row: any) => {
    let result = await reqSkuList(row.id)
    if (result.code == 200) {
        skuArr.value = result.data
        show.value = true
    }
}
// 删除已有的spu按钮的回调
const deleteSpu = async (row: any) => {
    let result = await rqRemoveSpu(row.id)
    if (result.code == 200) {
        ElMessage({ type: 'success', message: '删除成功' })
        getHasSpu(records.value.length > 1 ? pageNo.value : pageNo.value - 1)
    } else {
        ElMessage({ type: 'error', message: '删除失败' })
    }
}
// 路由组件销毁前，清空仓库关于分类的数据
onBeforeUnmount(() => {
    categoryStore.$reset()
})
</script>

<style scoped></style>